package cn.cs.controller;

import cn.cs.common.Dto;
import cn.cs.common.DtoUtil;
import cn.cs.common.EmptyUtils;
import cn.cs.pojo.CapitalPoolType;
import cn.cs.service.CapitalPoolTypeService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;


@RestController
@RequestMapping(value = "/capitalPoolType")
public class CapitalPoolTypeController {

    @Resource
    private CapitalPoolTypeService capitalPoolTypeService;

    /**
     * 分页查询 资金池事件分类
     *
     * @param pageNumber
     * @param pageSize
     * @return
     * @throws Exception TODO 添加搜索
     */
    @RequestMapping(value = "/csPlatform/getCapitalPoolTypeByPage")
    public Dto getCapitalPoolTypeByPage(Integer pageNumber, Integer pageSize) throws Exception {
        Map<String, Object> param = new HashMap<>();
        return DtoUtil.returnSuccess("获取成功", capitalPoolTypeService.queryCapitalPoolTypePageByMap(param, pageNumber, pageSize));
    }

    /**
     * 查询资金池事件分类
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/csPlatform/getCapitalPoolTypeAll")
    public Dto getCapitalPoolTypeAll() throws Exception {
        Map<String, Object> param = new HashMap<>();
        return DtoUtil.returnSuccess("获取成功", capitalPoolTypeService.getCapitalPoolTypeListByMap(param));
    }

    /**
     * 新增/编辑 资金池事件分类
     * @param capitalPoolType
     * @return
     */
    @RequestMapping(value = "/csPlatform/updateCapitalPoolType")
    public Dto updateCapitalPoolType(@RequestBody CapitalPoolType capitalPoolType) {
        try {
            String msg = "新增成功";
            if (EmptyUtils.isEmpty(capitalPoolType.getId())) {
                capitalPoolTypeService.addCapitalPoolType(capitalPoolType);
            } else {
                msg = "修改成功";
                capitalPoolTypeService.modifyCapitalPoolType(capitalPoolType);
            }
            return DtoUtil.returnSuccess(msg);
        } catch (Exception e) {
            e.printStackTrace();
            return DtoUtil.returnFail("操作失败，请重试", "001");
        }
    }



    /**
     * 删除 资金池事件分类
     * @param ids
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/csPlatform/delCapitalPoolType")
    public Dto delCapitalPoolType(String ids)throws Exception{
        int i = 0;
        for(String id:ids.split(",")){
            i += capitalPoolTypeService.deleteCapitalPoolTypeById(id);
        }
        if(i>0){
            return DtoUtil.returnSuccess("操作成功", i);
        }else {
            return DtoUtil.returnFail("操作失败，请重试", "001");
        }
    }



}
